package com.yjf.ljr.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.dataclass.HelpItemDataClass.HelpItemInfo;
import com.yjf.ljr.util.AppUtil;

public class HelpAdapter extends BAdapter {

	private Context mContext;
	private List<HelpItemInfo> mData;
	private LayoutInflater mInflater;

	public HelpAdapter(Context ctx, List<HelpItemInfo> data) {
		mContext = ctx;
		mData = data;
		mInflater = LayoutInflater.from(ctx);
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.item_help_title, null);
			holder = new ViewHolder();
			initComponents(mContext, convertView, holder);

			holder.ivHelpArrow.setTag("fold");
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final HelpItemInfo item = mData.get(position);

		AppUtil.setViewText(holder.tvHelpTitle, item.helpTitle);
		AppUtil.setViewHtml(holder.tvHelpDetail, item.helpDetail);

		final ViewHolder viewHolder = holder;
		// final int curPos = position;
		final ViewGroup vgParent = parent;
		holder.rlHelpTitle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if ("fold".equals(viewHolder.ivHelpArrow.getTag())) { // 展开
					for (int i = 0; i < vgParent.getChildCount(); i++) {
						final View vChild = vgParent.getChildAt(i);
						ImageView ivHelpArrow = (ImageView) vChild.findViewById(R.id.ivHelpArrow);
						if ("unfold".equals(ivHelpArrow.getTag())) {
							/*ivHelpArrow.setTag("fold");
							vChild.findViewById(R.id.llHelpDetail).setVisibility(View.GONE);*/

							/*****/

							Animation foldAnim = AnimationUtils.loadAnimation(mContext, R.anim.help_arrow_fold_rotate);
							ivHelpArrow.startAnimation(foldAnim);
							ivHelpArrow.setTag("fold");

							ScaleAnimation animaHide = new ScaleAnimation(1.0f, 1.0f, 1.0f, 0.0f,
									Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.0f);
							animaHide.setDuration(200);// 设置动画持续时间

							vChild.findViewById(R.id.llHelpDetail).startAnimation(animaHide);
							animaHide.setAnimationListener(new AnimationListener() {
								@Override
								public void onAnimationStart(Animation animation) {
								}

								@Override
								public void onAnimationRepeat(Animation animation) {
								}

								@Override
								public void onAnimationEnd(Animation animation) {
									vChild.findViewById(R.id.llHelpDetail).setVisibility(View.GONE);
								}
							});

							/***/
						}
					}
					Animation unfoldAnim = AnimationUtils.loadAnimation(mContext, R.anim.help_arrow_unfold_rotate);
					unfoldAnim.setFillAfter(true);
					viewHolder.ivHelpArrow.startAnimation(unfoldAnim);
					viewHolder.ivHelpArrow.setTag("unfold");
					viewHolder.llHelpDetail.setVisibility(View.VISIBLE);

					ScaleAnimation animShow = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF,
							0.5f, Animation.RELATIVE_TO_SELF, 0.0f);
					animShow.setDuration(200);// 设置动画持续时间
					viewHolder.llHelpDetail.startAnimation(animShow);
					// ((HelpActivity)mContext).mLvHelp.setSelection(curPos);
				} else if ("unfold".equals(viewHolder.ivHelpArrow.getTag())) {
					Animation foldAnim = AnimationUtils.loadAnimation(mContext, R.anim.help_arrow_fold_rotate);
					viewHolder.ivHelpArrow.startAnimation(foldAnim);
					viewHolder.ivHelpArrow.setTag("fold");

					ScaleAnimation animaHide = new ScaleAnimation(1.0f, 1.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF,
							0.5f, Animation.RELATIVE_TO_SELF, 0.0f);
					animaHide.setDuration(200);// 设置动画持续时间

					viewHolder.llHelpDetail.startAnimation(animaHide);
					animaHide.setAnimationListener(new AnimationListener() {
						@Override
						public void onAnimationStart(Animation animation) {
						}

						@Override
						public void onAnimationRepeat(Animation animation) {
						}

						@Override
						public void onAnimationEnd(Animation animation) {
							viewHolder.llHelpDetail.setVisibility(View.GONE);
						}
					});
				}
			}
		});
		return convertView;
	}

	private class ViewHolder {
		@ID("rlHelpTitle")
		RelativeLayout rlHelpTitle;
		@ID("tvHelpTitle")
		TextView tvHelpTitle;
		@ID("ivHelpArrow")
		ImageView ivHelpArrow;
		@ID("llHelpDetail")
		LinearLayout llHelpDetail;
		@ID("tvHelpDetail")
		TextView tvHelpDetail;
	}
}
